<template>
  <div>
    <h4>当前角色：{{ this.role.name }}</h4>
    <el-tree
        id="resTree"
        :data="columnList"
        :props="defaultProps"
        :node-key="this.columnList.id"
        accordion
        @node-click="handleNodeClick">
    </el-tree>

    <el-table
        :data="tableData"
        style="width: 100%;"
        row-key="id"
        border
        :tree-props="{children: 'children'}">
      <el-table-column
          prop="name1"
          label="一级">
      </el-table-column>
      <el-table-column
          prop="name2"
          label="二级">
      </el-table-column>
      <el-table-column
          prop="name3"
          label="三级">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  getColumnList
} from "@/api/authority/column"

export default {
  data() {
    return {
      role: this.$route.query,
      columnList: [],
      tableData: [
        {
          id: '',
          name1: '',
          name2: '',
          name3: '',
          children: []
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    // console.log(this.role)
    this.getColumn();
  },
  methods: {
    getColumn() {
      getColumnList().then(res => {
        res.rows.forEach(item => {
          if (item.pid === 1 || item.pid === 0) {
            this.columnList.push(item)
          }
        })
        console.log(this.columnList)
        // this.getTableData();
      });
    },
    getTableData() {
      this.columnList.forEach(item => {
        this.tableData[item.id-1].name1 = item.name
        if (item.children.length > 0) {
          item.children.forEach(item1 => {
            this.tableData[item.id-1].name2 = item1.name
            if (item1.children.length > 0) {
              item1.children.forEach(item2 => {
                this.tableData[item.id-1].name3 = item2.name
              })
            }
          })
        }
      })
      console.log(this.tableData)
    },
    handleNodeClick(data) {
      console.log(data);
    }
  }
}
</script>

<style scoped>
#resTree {
  padding: 10px;
}
</style>